{% extends "base.html" %}
{% load sorter %}{% load iptocountry_flag %}
{% block content %}
<table>
  <thead>
    <th>{% sorter "title" "Title" %}</th> 
    <th>{% sorter "user" "Author" %}</th> 
    <th>{% sorter "pub_date" "Date Published" %}</th>
  </thead>
{% for object in objects.object_list %}
    {# Each "object" is a Object model object. #}
    <tr>
      <td>
	<strong>{{ object.title }}</strong><br/><a href="{{ object.get_absolute_url }}">View</a> 
	| <a href="{% url poll_modify object.pk %}">Edit</a>
	| <a href="{% url poll_delete object.pk %}">Delete</a>
      </td> 
      <td>
	{{ object.user }}{% get_flag object.ip_address as flag %}
	<img src="{{ flag.flag_url }}" alt="{{ flag.COUNTRY_NAME|lower }}" />
      </td>
      <td>{{ object.pub_date|date:"d/m/y" }}</td>
    </tr>
{% endfor %}
</table>
<div class="pagination">
    <span class="step-links">
        {% if objects.has_previous %}
            <a href="?page={{ objects.previous_page_number }}">previous</a>
        {% endif %}
        <span class="current">
            Page {{ objects.number }} of {{ objects.paginator.num_pages }}.
        </span>
        {% if objects.has_next %}
            <a href="?page={{ objects.next_page_number }}">next</a>
        {% endif %}
    </span>
</div>
{% endblock %}

{% block code %}
<span class="k">class</span> <span class="nc">PaginatedView</span><span class="p">(</span><span class="n">View</span><span class="p">):</span>
    <span class="n">defaults</span> <span class="o">=</span> <span class="p">{</span><span class="s">&quot;per_page&quot;</span><span class="p">:</span> <span class="mi">25</span><span class="p">}</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">PaginatedView</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">model</span>
        
    <span class="k">def</span> <span class="nf">build_params</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">page</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">order_by</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="n">per_page</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s">&quot;per_page&quot;</span><span class="p">]</span>
        <span class="c"># cast page as int. do not catch ValueError (forced by URL)</span>
        <span class="n">page</span> <span class="o">=</span> <span class="n">page</span> <span class="ow">and</span> <span class="nb">int</span><span class="p">(</span><span class="n">page</span><span class="p">)</span> <span class="ow">or</span> <span class="mi">1</span>
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">objects</span><span class="p">,</span> <span class="s">&#39;published&#39;</span><span class="p">):</span>
            <span class="n">object_list</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">published</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">object_list</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
        <span class="c"># order. Again, value filtered by URL</span>
        <span class="k">if</span> <span class="n">order_by</span><span class="p">:</span>
            <span class="n">object_list</span> <span class="o">=</span> <span class="n">object_list</span><span class="o">.</span><span class="n">order_by</span><span class="p">(</span><span class="n">order_by</span><span class="p">)</span>
        <span class="n">paginator</span> <span class="o">=</span> <span class="n">Paginator</span><span class="p">(</span><span class="n">object_list</span><span class="p">,</span> <span class="n">per_page</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">objects</span> <span class="o">=</span> <span class="n">paginator</span><span class="o">.</span><span class="n">page</span><span class="p">(</span><span class="n">page</span><span class="p">)</span>
        <span class="k">except</span> <span class="p">(</span><span class="n">EmptyPage</span><span class="p">,</span> <span class="n">InvalidPage</span><span class="p">):</span>
            <span class="n">objects</span> <span class="o">=</span> <span class="n">paginator</span><span class="o">.</span><span class="n">page</span><span class="p">(</span><span class="n">paginator</span><span class="o">.</span><span class="n">num_pages</span><span class="p">)</span>
        <span class="c"># return built params</span>
        <span class="k">return</span> <span class="p">{</span><span class="s">&quot;objects&quot;</span><span class="p">:</span> <span class="n">objects</span><span class="p">,</span>
                <span class="s">&quot;sort_field&quot;</span><span class="p">:</span> <span class="n">order_by</span><span class="p">}</span>

<span class="c"># POLLS #########################################################</span>
<span class="k">class</span> <span class="nc">PaginatedPollView</span><span class="p">(</span><span class="n">PaginatedView</span><span class="p">):</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s">&quot;polls/poll_list.html&quot;</span>
    <span class="n">defaults</span> <span class="o">=</span> <span class="p">{</span><span class="s">&quot;base_url&quot;</span><span class="p">:</span> <span class="s">&quot;poll_list&quot;</span><span class="p">}</span>
{% endblock %}
